/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

class mysqlBridge {

    public Connection conn;
    public Statement stm;
    public String host;
    public String db_name;
    public String username;
    public String password;

    public mysqlBridge() {
        if (this.getOSName().compareTo("windows") == 0) {
            this.setParameters("localhost", "java_mysql", "webmin", "abc123");
        } else {
            this.setParameters("localhost", "java_mysql", "root", "");
        }

        try {
            Class c = Class.forName("com.mysql.jdbc.Driver");
            String db_url = "jdbc:mysql://" + this.host + ":3306/" + this.db_name;
            this.conn = DriverManager.getConnection(db_url, this.username, this.password);

            this.stm = this.conn.createStatement();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public String getOSName() {
        String os = System.getProperty("os.name").toLowerCase();
        if (os.indexOf("windows") != -1)
            return "windows";
        if (os.indexOf("linux") != -1)
            return "linux";
        return null;
    }

    public void setParameters(String db_host, String db_name, String db_user, String db_pass) {
        this.host = db_host;
        this.db_name = db_name;
        this.username = db_user;
        this.password = db_pass;
    }

    public void closeConnection() {
        try {
            this.conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public ResultSet getBlogRecord(String blogID) {
        try {
            String query = "SELECT * FROM `blog_list` WHERE" +
                    " `blogID`='" + blogID + "'";

            ResultSet resl = this.stm.executeQuery(query);
            return resl;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *
     * @param table_name
     * @param col1
     * @param col1_val
     * @param col2
     * @param col2_val
     */
    public void addToDB(String table_name, String col1, String col1_val, String col2, String col2_val) {
        try {
            ResultSet rel = this.getBlogRecord(col1_val);
            if (!rel.next()) {
                String query = "INSERT INTO `" + table_name + "` (`" + col1 + "`,`" + col2 + "`) VALUES" +
                        " ('" + col1_val + "', '" + col2_val + "')";

                this.stm.executeUpdate(query);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

public class getToDB {
    public static void main(String args[]) {
        mysqlBridge imple = new mysqlBridge();

        imple.addToDB("blog_list", "blogID", args[0], "friends", "");

        imple.closeConnection();
    }
}
